import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/Home.vue'),
      children: [
        { // 首页
          path: '/',
          name: 'homenav',
          component: () => import('../views/Homenav.vue')
        },
        { // 个人中心
          path: '/personal',
          name: 'Personal',
          component: () => import('../views/Personal/Personal.vue')
        },
        { // 购物车
          path: '/shopping',
          name: 'Shopping',
          component: () => import('../views/Shopping/Shopping.vue')
        },
        { // 咨询
          path: '/seek',
          name: 'seek',
          component: () => import('../views/seek/seek.vue')
        },
        { // 社区交流平台
          path: '/community',
          name: 'community',
          component: () => import('../views/community/community.vue')
        },
      ]
    },
    { // 登录
      path: '/login',
      name: 'login',
      component: () => import('../views/Loginfunction/Login.vue')
    },
    { // 注册
      path: '/register',
      name: 'Register',
      component: () => import('../views/Loginfunction/Register.vue')
    },

  ]
})
// Vue前置路由
router.beforeEach((to, from, next) => {
  const Token = localStorage.getItem('token')
  if (Token) {
    next()
  } else {
    if (to.path === '/Personal' || to.path === '/shopping' || to.path === '/community') {
      next({
        path: '/login'
      })
    } else {
      next()
    }
  }
})


export default router
